//
// Created by wenbo on 24-9-7.
//

#ifndef __SENSOR_FILTER_H__
#define __SENSOR_FILTER_H__

#ifdef __cplusplus
extern "C"
{
#endif

#define FILTER_NUM 8 // //滑动平均滤波数值个??

#include "stdint.h"

#pragma pack(1)
    typedef struct movingAverage_s
    {
        float *buffer;        /**< Data buffer pointer */
        uint16_t size;        /**< Size of filter buffer */
        uint16_t index;       /**< Current location in buffer */
        uint16_t fill;        /**< Buffer filled level */
        float sum;            /**< Cumulative value of buffer */
        float filtered;       /**< Filtered output */
        uint16_t sample_time; /**< data sampling time interval */
        uint32_t last_time;   /**< last sampled time */
        // uint8_t update_flag;
    } movingAverage_t;
#pragma pack()


    /**
     * @brief filter object initialization
     *
     * @param context [in] instance of filter object
     * @param filter_size [in] size of filter buffer
     * @param sample_time [in] filter sampling time in ms
     */
    void moving_average_create(movingAverage_t *context, uint16_t filter_size, uint16_t sample_time);

    /**
     * @brief filter process function
     *
     * @param context [in] instance of filter object
     * @param input [in] data sample to filter
     */
    void moving_average_filter(movingAverage_t *context, float input);


#pragma pack(1)
    typedef struct
    {
        /* data */
        float LastP; // 上一次估计的协方?
        float Now_P; // 当前估?的协方?
        float out; // 输出
        float Kg; // 卡尔曼?益
        float Q; // 过程?声协方差
        float R; // 测量?声协方差
    }FirstOrderKalmanFilter;
#pragma pack()


    void FirstOrder_KalmanFilter_Init(FirstOrderKalmanFilter *KalmanFilter,float Q,float R);
    float FirstOrder_KalmanFilter_Update(FirstOrderKalmanFilter *KalmanFilter,float input);



#ifdef __cplusplus
}
#endif

#endif /* __FILTER_H__ */
